<?php

/**
 * Формы прав/групп/пользователей.
 */
class UserareaForm extends Zend_Form {

    /**
     * Регистрационная форма
     *
     * @return UserareaForm 
     */
    function register() {

        $this->addElementPrefixPath('Model_User_Form_Validate',
                APP_PATH . "/forms/validate", 'validate');

        $this->setName('userRegisterForm');
        $this->setDefaultTranslator(Zend_Registry::get('Zend_Locale'));
        $config = Zend_Registry::get('config');

        $name = new Zend_Form_Element_Text('name');
        $name->setLabel('Name')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('Alnum', true, array(true));

        $login = new Zend_Form_Element_Text('login');
        $login->setLabel('Login')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('Alnum')
                ->addValidator('StringLength', false, $config->pass_lenght)
                ->addValidator('Db_NoRecordExists', false,
                        array(
                            array('table' => 'users',
                                'field' => 'login')
                ));


        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password')
                ->setRequired(true)
                ->setValue(null)
                ->addValidator('StringLength', false, $config->pass_lenght);


        $confirmPassword = new Zend_Form_Element_Password('password2');
        $confirmPassword->setLabel('Confirm password')
                ->setRequired(true)
                ->setValue(null)
                ->addValidator('StringLength', false, $config->pass_lenght)
                ->addValidator('PasswordConfirmation');

        $email = new Zend_Form_Element_Text('email');
        $email->setLabel('Email')
                ->setRequired(true)
                ->setValue(null)
                ->addValidator('StringLength', false, array(6))
                ->addValidator('EmailAddress')
                ->addValidator('Db_NoRecordExists', false,
                        array(
                            array('table' => 'users', 'field' => 'email')
                ));

        $captcha = new Zend_Form_Element_Captcha('captcha', array(
                    'captcha' => array(
                        'captcha' => 'Image',
                        'wordLen' => 6,
                        'width' => 260,
                        'timeout' => 120,
                        'expiration' => 300,
                        'font' => PUBLIC_PATH . '/fonts/captcha.ttf',
                        'imgDir' => PUBLIC_PATH . '/tmp/',
                        'imgUrl' => '/tmp/',
                        'gcFreq' => 5
                    )
                ));
        $captcha->setLabel('Captcha');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Register');

        $this->addElements(array($name, $login, $password, $confirmPassword,
            $email, $captcha, $submit));

        return $this;
    }

    /**
     * Шаблоны писем
     */
    public function templates() {
        $this->setName('userRegisterForm');
        $this->setDefaultTranslator(Zend_Registry::get('Zend_Locale'));

        $active = new Zend_Form_Element_Textarea('successful_registration');
        $active->setLabel('Successful registration')
                ->addFilter('StringTrim')
                ->setRequired();

        $confirm = new Zend_Form_Element_Textarea('confirm_registration');
        $confirm->setLabel('Confirm registration')
                ->addFilter('StringTrim')
                ->setRequired();

        $password = new Zend_Form_Element_Textarea('confirm_password');
        $password->setLabel('Confirm send password')
                ->addFilter('StringTrim')
                ->setRequired();

        $newpass = new Zend_Form_Element_Textarea('new_password');
        $newpass->setLabel('New password')
                ->addFilter('StringTrim')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');

        $this->addElements(
                array(
                    $active,
                    $confirm,
                    $password,
                    $newpass,
                    $submit
        ));

        $this->addDisplayGroup(
                array(
                    'confirm_registration',
                    'successful_registration',
                    'confirm_password',
                    'new_password',
                    'submit'
                ),
                'mails',
                array('legend' => _('Mail templates')));

        $this->setDisplayGroupDecorators(array(
            'FormElements',
            'Fieldset'
        ));
        return $this;
    }

}
